/**
 *
 * @author Jaime Moreno 
 *  Developed as a team based project for a course at the California State  University of Monterey Bay by Reynold Rondez, Jaime Moreno, Chris  Sterling, Eric McDonald
 *  SimpleLoop main program example using Java 2D for start of project for teambowser
 * just a line test to see how editing in browser works
 * just adding another test line
 * Test of moving sprite using keyboard input - goal for next week.
 * Use keys to move block around screen. Use R, G, B, W to change color of background.
 */


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*; // for Java 2D stuff
import java.applet.*;
import java.net.URL;
import java.util.*;

public class DungeonMaster extends Applet implements Runnable,KeyListener, MouseListener   {

	 /**
	 * 
	 */
	private static final long serialVersionUID = 7645822198859243768L;

	//random number generator
    Random rand = new Random();

    //the main thread
    Thread thread;

    //count the number of rectangles drawn
    long count = 0;
    
    //here's the shape used for drawing
    private Shape shape;
    private Color shapeColor;
    
    
    Graphics2D g2d;
    
    //Image Used to Replace Rectangle
    private Image playerImage;
    
    
    
    
    
    // current background color for the applet 
    private Color backColor; 

    int x = 0; // movement in x direction
    int y = 0; // movement in y direction
    
    // this method overrides the init method from the Applet class 
    //applet init event since an applet uses init instead of main like a regular java program
    public void init() {
        shape = new Rectangle2D.Double(-1.0, -1.0, 20.0, 20.0);
        playerImage = getImage(getURL("LinkMoveRight.gif"));
        
        addKeyListener(this); 
    }
 
    //applet start event
    public void start() {
        thread = new Thread(this);
        thread.start();
    }

    //thread run event
    public void run() {
        long start = System.currentTimeMillis();

        //acquire the current thread
        Thread current = Thread.currentThread();

        //here's the new game loop
        while (current == thread) {
            try {
                Thread.sleep(0);
            }
            catch(InterruptedException e) {
                e.printStackTrace();
            }

            //draw something
            repaint();

            //figure out how fast it's running
            if (start + 1000 < System.currentTimeMillis()) {
                start = System.currentTimeMillis();
                showStatus("x position: " + x + "\n"
                		   + "  y_position: " + y);
                count = 0;
            }
        }
    }

    //applet stop event
    public void stop() {
        thread = null;
    }

    //applet update event
    public void update(Graphics g) {
        paint(g);
    }

    //applet paint event
    public void paint(Graphics g) {
       
     // paints the rectangle at the updated position 
     // not using java2d then using java2d
       /*  g.setColor(backColor); // set background color
        g.fillRect(0,0,this.getWidth(), this.getHeight());

        g.setColor(Color.BLACK); // set block to black
        g.fillRect(r.x, r.y, r.width, r.height); 
        */
    	
    	//create an instance of Graphics2D
        Graphics2D g2d = (Graphics2D)g;

        //save the identity transform
        AffineTransform identity = new AffineTransform();


        //save the window width/height
        int width = getSize().width;
        int height = getSize().height;

        //fill the background with black
        g2d.setColor(backColor);
        g2d.fillRect(0, 0, width, height);

        //reset Graphics2D to the identity transform
        g2d.setTransform(identity);

        //move, rotate, and scale the shape according to key user pressed
        g2d.translate(x, y);


        //draw the shape with a color
        g2d.setColor(shapeColor);
        //g2d.fill(shape);
        g2d.drawImage(playerImage, 0, 0, this);

    }

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
	     // for this method, use the key code to generate movement 
	     int keyCode = e.getKeyCode(); 
	     
	     // move the rectangle 
	     if(keyCode == KeyEvent.VK_LEFT) 
	     { 
	          x -= 5; 
	          if(x < 0) x = 0; // minimal checking to make sure block doesn't go off left side of screen
	          playerImage = getImage(getURL("LinkMoveLeft.gif"));
	    	  g2d.drawImage(playerImage, x, y, this);
	          repaint();
	     } 
	     
	     else if(keyCode == KeyEvent.VK_RIGHT) 
	     { 
	    	  
	          x += 5; 
	          
	          // make sure we don't go off to the right
	          if(x > getSize().width-shape.getBounds().width)  
	          { 
	               x = getSize().width-shape.getBounds().width; 

	          } 
	          playerImage = getImage(getURL("LinkMoveRight.gif"));
	    	  g2d.drawImage(playerImage, x, y, this);
	          repaint(); 
	     } 
	     
	     else if(keyCode == KeyEvent.VK_UP) 
	     { 
	    	  
	          y -= 5; 
	          
	          // can't go off top
	          if(y < 0){
	        	  y = 0;   
	          }
	          playerImage = getImage(getURL("LinkMoveUp.gif"));
	    	  g2d.drawImage(playerImage, x, y, this);
	          repaint(); 
	     } 

	     else if(keyCode == KeyEvent.VK_DOWN) 
	     { 
	          y += 5; 
	          
	          // can't go off bottom
	          if(y > getSize().height-shape.getBounds().height) 
	          { 
	               y = getSize().height-shape.getBounds().height; 
	          } 
	    	  playerImage = getImage(getURL("LinkMoveDown.gif"));
	    	  g2d.drawImage(playerImage, x, y, this);
	          repaint(); 
	     } 
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		 // for this method, use the actual key char to call action 
	     char keyChar = e.getKeyChar(); 

	     // change the background color 
	     switch(keyChar) 
	     {
			case 'r': 
			{
			      backColor = Color.RED; 
			      g2d.setColor(backColor);
			      repaint(); 
			      break; 
			} 
		
			case 'g': 
			{ 
			      backColor = Color.GREEN; 
			      g2d.setColor(backColor);
			      repaint(); 
			      break; 
			} 
		
			case 'b': 
			{ 
		
			      backColor = Color.BLUE; 
			      g2d.setColor(backColor);
			      repaint(); 
			      break; 
			} 
			
			case 'w': 
			{ 
			      backColor = Color.WHITE; 
			      g2d.setColor(backColor);
			      repaint(); 
			      break; 
			} 
	     } // end switch
		
	}

	private URL getURL(String filename){
		URL url = null;
		
		try{
			url = this.getClass().getResource(filename);
		}
		catch (Exception e){}
		
		return url;
	}
}
